﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
//这个公共类目前有个问题，
//public static void FillDataTable(DataTable dtNew, DataSet ds, Dictionary<string, DataRow> dicFacName2DataRow)
//dicFacName2DataRow不对,应该是dicContractName2DataRow，因为在工厂录入界面，可能有多个合同号对应同一个工厂的情况

//FeeFacInvoiceRelationTab这个表也有问题，
//不应该 FacFeeTabID 和	FeeInvoiceTabID 
//FacFeeTabID对应的是合同号，应该是FactoryDataTabID 和	FeeInvoiceTabID 
namespace BYManageInfoSystem
{
    public class Fee_HelpClass
    {
        private static DGBY.BLL.FeeTab m_bllFeeTab = new DGBY.BLL.FeeTab();
        private static DGBY.BLL.CargoBoxTab m_bllCargoBoxTab = new DGBY.BLL.CargoBoxTab();
        private static DGBY.BLL.FacFeeRelationTab m_bllFacFeeRelationTab = new DGBY.BLL.FacFeeRelationTab();
        
        public static void MakeColumnNew(DataTable dt)
        {
            dt.Columns.Add("跟单员", System.Type.GetType("System.String"));
            dt.Columns.Add("工厂简称", System.Type.GetType("System.String"));
            dt.Columns.Add("订单号", System.Type.GetType("System.String"));
            dt.Columns.Add("合同号", System.Type.GetType("System.String"));
            //dt.Columns.Add("经营单位", System.Type.GetType("System.String"));
            dt.Columns.Add("柜号", System.Type.GetType("System.String"));
            dt.Columns.Add("柜数", System.Type.GetType("System.Int32"));
           
            dt.Columns.Add("百业装卸发票", System.Type.GetType("System.Decimal"));
            dt.Columns.Add("百业收据", System.Type.GetType("System.Decimal"));
            dt.Columns.Add("拖车行发票", System.Type.GetType("System.Decimal"));
            dt.Columns.Add("纸箱证收据", System.Type.GetType("System.Decimal"));
            dt.Columns.Add("检测费发票", System.Type.GetType("System.Decimal"));

            dt.Columns.Add("应收费用合计", System.Type.GetType("System.Decimal"));
            dt.Columns.Add("应付费用合计", System.Type.GetType("System.Decimal"));

            //商检
            dt.Columns.Add("商检金额", System.Type.GetType("System.Decimal"));
            dt.Columns.Add("应收商检费", System.Type.GetType("System.Decimal"));
            dt.Columns.Add("应付商检费合计", System.Type.GetType("System.Decimal"));

            //大陆运输
            dt.Columns.Add("大陆运输公司", System.Type.GetType("System.String"));
            dt.Columns.Add("装柜镇区", System.Type.GetType("System.String"));
            dt.Columns.Add("应付大陆运输费合计", System.Type.GetType("System.Decimal"));
            DataSet ds = m_bllFeeTab.GetFeeNameList("FeeCateTab.FeeCateName like " + "'%" + "大陆运输" + "%'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    string strTemp = ds.Tables[0].Rows[i]["FeeName"].ToString();
                    dt.Columns.Add(strTemp, System.Type.GetType("System.Decimal"));
                }
            }
            dt.Columns.Add("应收大陆运输费合计", System.Type.GetType("System.Decimal"));

            //香港运输
            dt.Columns.Add("香港运输公司", System.Type.GetType("System.String"));
            dt.Columns.Add("香港应收运输费合计", System.Type.GetType("System.Decimal"));
            dt.Columns.Add("香港应付运输费合计", System.Type.GetType("System.Decimal"));
            
            //报关
            dt.Columns.Add("报关行", System.Type.GetType("System.String"));
            dt.Columns.Add("港口", System.Type.GetType("System.String"));
            dt.Columns.Add("报关单号", System.Type.GetType("System.String"));
            dt.Columns.Add("核销单号", System.Type.GetType("System.String"));
            dt.Columns.Add("报关金额", System.Type.GetType("System.Decimal"));
            dt.Columns.Add("币种", System.Type.GetType("System.String"));
            dt.Columns.Add("经营单位", System.Type.GetType("System.String"));
            dt.Columns.Add("申报日期", System.Type.GetType("System.DateTime"));
            dt.Columns.Add("应付报关费", System.Type.GetType("System.Decimal"));

            ds = m_bllFeeTab.GetFeeNameList("FeeCateTab.FeeCateName like " + "'%" + "报关" + "%'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    string strTemp = ds.Tables[0].Rows[i]["FeeName"].ToString();
                    dt.Columns.Add(strTemp, System.Type.GetType("System.Decimal"));
                }
            }
            dt.Columns.Add("应收报关费合计", System.Type.GetType("System.Decimal"));
            //大陆船务费
            dt.Columns.Add("应收大陆船务费合计", System.Type.GetType("System.Decimal"));
            dt.Columns.Add("应付大陆船务费合计", System.Type.GetType("System.Decimal"));
            //香港船务费
            dt.Columns.Add("应收香港船务费合计", System.Type.GetType("System.Decimal"));
            dt.Columns.Add("应付香港船务费合计", System.Type.GetType("System.Decimal"));

            //单证
            dt.Columns.Add("单证公司名称", System.Type.GetType("System.String"));
            dt.Columns.Add("应付单证费用合计", System.Type.GetType("System.Decimal"));
            ds = m_bllFeeTab.GetFeeNameList("FeeCateTab.FeeCateName like " + "'%" + "单证" + "%'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    string strTemp = ds.Tables[0].Rows[i]["FeeName"].ToString();
                    dt.Columns.Add(strTemp, System.Type.GetType("System.Decimal"));
                }
            }

            dt.Columns.Add("应收单证费合计", System.Type.GetType("System.Decimal"));

            //DHL
            dt.Columns.Add("应收DHL费用合计", System.Type.GetType("System.Decimal"));
            dt.Columns.Add("应付DHL费用合计", System.Type.GetType("System.Decimal"));
            //纸箱证
            dt.Columns.Add("应收纸箱证费用合计", System.Type.GetType("System.Decimal"));
            dt.Columns.Add("应付纸箱证费用合计", System.Type.GetType("System.Decimal"));
            //检测
            dt.Columns.Add("应收检测费用合计", System.Type.GetType("System.Decimal"));
            dt.Columns.Add("应付检测费用合计", System.Type.GetType("System.Decimal"));
        }

        public static void FillDataTableNew(DataTable dtNew
            , DataSet ds, DataSet dsFees, Dictionary<string, DataRow> dicContractNo2DataRow)
        {
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                List<string> lstDLYS = new List<string>();
                List<string> lstBG = new List<string>();
                List<string> lstDZ = new List<string>();

                DataSet dsTemp = m_bllFeeTab.GetFeeNameList("FeeCateTab.FeeCateName like " + "'%" + "大陆运输" + "%'");
                if (dsTemp.Tables.Count > 0 && dsTemp.Tables[0].Rows.Count > 0)
                {
                    for (int i = 0; i < dsTemp.Tables[0].Rows.Count; i++)
                    {
                        string strTemp = dsTemp.Tables[0].Rows[i]["FeeName"].ToString();
                        lstDLYS.Add(strTemp);
                    }
                }

                dsTemp = m_bllFeeTab.GetFeeNameList("FeeCateTab.FeeCateName like " + "'%" + "报关" + "%'");
                if (dsTemp.Tables.Count > 0 && dsTemp.Tables[0].Rows.Count > 0)
                {
                    for (int i = 0; i < dsTemp.Tables[0].Rows.Count; i++)
                    {
                        string strTemp = dsTemp.Tables[0].Rows[i]["FeeName"].ToString();
                        lstBG.Add(strTemp);
                    }
                }

                dsTemp = m_bllFeeTab.GetFeeNameList("FeeCateTab.FeeCateName like " + "'%" + "单证" + "%'");
                if (dsTemp.Tables.Count > 0 && dsTemp.Tables[0].Rows.Count > 0)
                {
                    for (int i = 0; i < dsTemp.Tables[0].Rows.Count; i++)
                    {
                        string strTemp = dsTemp.Tables[0].Rows[i]["FeeName"].ToString();
                        lstDZ.Add(strTemp);
                    }
                }

                Dictionary<string, int> dicFeeName2Index = new Dictionary<string, int>();

                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    int nFeeFacID = (int)ds.Tables[0].Rows[i]["FacFeeTabID"];
                    DataRow dr = dtNew.NewRow();
                    string strGuiNo = ds.Tables[0].Rows[i]["集装箱柜号"].ToString();
                    //strGuiNo = "ddd,ssss,";
                    string[] arr = strGuiNo.Split(',');
                    string tempa = "";
                    int nGuiCount = 0;
                    foreach (string ch in arr)
                    {

                        if (ch != "")
                        {
                            if (nGuiCount == 0)
                                tempa += ch;
                            else
                                tempa += "\n" + ch;
                        }

                        nGuiCount++;
                    }
                    if (nGuiCount > 1)
                    {
                        dr["柜数"] = nGuiCount - 1;
                        dr["柜号"] = tempa;
                    }
                    else
                    {
                        dr["柜数"] = 0;
                        dr["柜号"] = "";
                    }



                    dr["跟单员"] = ds.Tables[0].Rows[i]["跟单人员"];
                    dr["工厂简称"] = ds.Tables[0].Rows[i]["工厂名称"];
                    dr["订单号"] = ds.Tables[0].Rows[i]["订单号"];
                    dr["合同号"] = ds.Tables[0].Rows[i]["合同编号"];
                    //dr["经营单位"] = ds.Tables[0].Rows[i]["经营单位"];

                           
                    dr["商检金额"] = ds.Tables[0].Rows[i]["商检金额"];
                    dr["应收商检费"] = ds.Tables[0].Rows[i]["应收商检费"];
                    dr["应付商检费合计"] = ds.Tables[0].Rows[i]["应付商检费"];

                    dr["大陆运输公司"] = ds.Tables[0].Rows[i]["运输公司"];
                    dr["装柜镇区"] = ds.Tables[0].Rows[i]["装柜镇区"];
                    dr["应付大陆运输费合计"] = ds.Tables[0].Rows[i]["应付大陆运输费"];

                    
                    dr["香港运输公司"] = ds.Tables[0].Rows[i]["香港运输公司"];
                    dr["香港应付运输费合计"] = ds.Tables[0].Rows[i]["应付香港运输费"];
                    dr["香港应收运输费合计"] = ds.Tables[0].Rows[i]["应收香港运输费"];

                    dr["报关行"] = ds.Tables[0].Rows[i]["报关行名称"];
                    dr["港口"] = ds.Tables[0].Rows[i]["装运港"];
                    dr["报关单号"] = ds.Tables[0].Rows[i]["报关单号"];
                    dr["核销单号"] = ds.Tables[0].Rows[i]["核销单号"];
                    dr["报关金额"] = ds.Tables[0].Rows[i]["报关金额"];
                    dr["币种"] = ds.Tables[0].Rows[i]["币种"];
                    dr["经营单位"] = ds.Tables[0].Rows[i]["币种"];
                    dr["申报日期"] = ds.Tables[0].Rows[i]["申报日期"];
                    dr["应付报关费"] = ds.Tables[0].Rows[i]["应付报关费"];
                    


                    dr["应收大陆船务费合计"] = ds.Tables[0].Rows[i]["应收大陆船务费"];
                    dr["应付大陆船务费合计"] = ds.Tables[0].Rows[i]["应付大陆船务费"];


                    dr["应收香港船务费合计"] = ds.Tables[0].Rows[i]["应收香港船务费"];
                    dr["应付香港船务费合计"] = ds.Tables[0].Rows[i]["应付香港船务费"];

                    dr["单证公司名称"] = ds.Tables[0].Rows[i]["单证公司"];
                    dr["应付单证费用合计"] = ds.Tables[0].Rows[i]["应付单证费"];

                    dr["应收DHL费用合计"] = ds.Tables[0].Rows[i]["应收DHL费"];
                    dr["应付DHL费用合计"] = ds.Tables[0].Rows[i]["应付DHL费"];
                    dr["应收纸箱证费用合计"] = ds.Tables[0].Rows[i]["应收纸箱证费"];
                    dr["应付纸箱证费用合计"] = ds.Tables[0].Rows[i]["应付纸箱证费"];
                    dr["应收检测费用合计"] = ds.Tables[0].Rows[i]["应收检测费"];
                    dr["应付检测费用合计"] = ds.Tables[0].Rows[i]["应付检测费"];


                    dicFeeName2Index.Clear();
                    DataRow[] Rows = dsFees.Tables[0].Select("FacFeeTabID = '" + nFeeFacID + "'" 
                        + " and 费用种类 like " + "'%" + "报关" + "%'");
                    for (int j = 0; j < Rows.Length; j++)
                    {
                        string strFeeName = Rows[j]["费用名称"].ToString();
                        dicFeeName2Index.Add(strFeeName, j);
                    }

                    Decimal dtTotalTemp = 0;
                    foreach (string strTemp in lstBG)
                    {
                        Decimal dtTemp = 0;
                        if (dicFeeName2Index.Keys.Contains(strTemp))
                        {
                            dtTemp = (Decimal)Rows[dicFeeName2Index[strTemp]]["费用金额"];
                        }

                        dr[strTemp] = dtTemp;
                        dtTotalTemp += dtTemp;
                    }
                    dr["应收报关费合计"] = dtTotalTemp;


                    dicFeeName2Index.Clear();
                    Rows = dsFees.Tables[0].Select("FacFeeTabID = '" + nFeeFacID + "'"
                        + " and 费用种类 like " + "'%" + "大陆运输" + "%'");
                    for (int j = 0; j < Rows.Length; j++)
                    {
                        string strFeeName = Rows[j]["费用名称"].ToString();
                        dicFeeName2Index.Add(strFeeName, j);
                    }

                    dtTotalTemp = 0;
                    foreach (string strTemp in lstDLYS)
                    {
                        Decimal dtTemp = 0;
                        if (dicFeeName2Index.Keys.Contains(strTemp))
                        {
                            dtTemp = (Decimal)Rows[dicFeeName2Index[strTemp]]["费用金额"];
                        }

                        dr[strTemp] = dtTemp;
                        dtTotalTemp += dtTemp;
                    }
                    dr["应收大陆运输费合计"] = dtTotalTemp;


                    dicFeeName2Index.Clear();
                    Rows = dsFees.Tables[0].Select("FacFeeTabID = '" + nFeeFacID + "'"
                        + " and 费用种类 like " + "'%" + "单证" + "%'");
                    for (int j = 0; j < Rows.Length; j++)
                    {
                        string strFeeName = Rows[j]["费用名称"].ToString();
                        dicFeeName2Index.Add(strFeeName, j);
                    }

                    dtTotalTemp = 0;
                    foreach (string strTemp in lstDZ)
                    {
                        Decimal dtTemp = 0;
                        if (dicFeeName2Index.Keys.Contains(strTemp))
                        {
                            dtTemp = (Decimal)Rows[dicFeeName2Index[strTemp]]["费用金额"];
                        }

                        dr[strTemp] = dtTemp;
                        dtTotalTemp += dtTemp;
                    }
                    dr["应收单证费合计"] = dtTotalTemp;

                    dr["百业装卸发票"] = (Decimal)dr["应收商检费"] 
                        + (Decimal)dr["应收报关费合计"] + (Decimal)dr["应收大陆船务费合计"];

                    dr["百业收据"] = (Decimal)dr["应收单证费合计"]
                        + (Decimal)dr["应收DHL费用合计"] + (Decimal)dr["香港应收运输费合计"]
                        + (Decimal)dr["应收香港船务费合计"];

                    dr["拖车行发票"] = dr["应收大陆运输费合计"];
                    dr["纸箱证收据"] = dr["应收纸箱证费用合计"];
                    dr["检测费发票"] = dr["应收检测费用合计"];

                    dr["应收费用合计"] = (Decimal)dr["百业收据"] + (Decimal)dr["百业装卸发票"]
                        + (Decimal)dr["拖车行发票"] + (Decimal)dr["纸箱证收据"] + (Decimal)dr["检测费发票"];




                    dr["应付费用合计"] = (Decimal)dr["应付大陆船务费合计"]
                        + (Decimal)dr["应付香港船务费合计"] + (Decimal)dr["香港应付运输费合计"]
                        + (Decimal)dr["应付商检费合计"] + (Decimal)dr["应付大陆运输费合计"]
                        + (Decimal)dr["应付DHL费用合计"] + (Decimal)dr["应付纸箱证费用合计"]
                        + (Decimal)dr["应付检测费用合计"]
                        + (Decimal)dr["应付报关费"] + (Decimal)dr["应付单证费用合计"];


                    dtNew.Rows.Add(dr.ItemArray);

                    if (dicContractNo2DataRow != null)
                    {
                        dicContractNo2DataRow.Add(ds.Tables[0].Rows[i]["合同编号"].ToString(), dr);
                    }
                }
            }
        }

        public static void MakeColumn(DataTable dt)
        {
            dt.Columns.Add("工厂简称", System.Type.GetType("System.String"));
            dt.Columns.Add("订单号", System.Type.GetType("System.String"));
            dt.Columns.Add("合同号", System.Type.GetType("System.String"));
            dt.Columns.Add("柜号", System.Type.GetType("System.String"));
            dt.Columns.Add("柜数", System.Type.GetType("System.Int32"));

            dt.Columns.Add("百业收据", System.Type.GetType("System.Decimal"));
            dt.Columns.Add("百业装卸发票", System.Type.GetType("System.Decimal"));
            dt.Columns.Add("运输发票", System.Type.GetType("System.Decimal"));
            dt.Columns.Add("应收费用合计", System.Type.GetType("System.Decimal"));
            dt.Columns.Add("应付费用", System.Type.GetType("System.Decimal"));

            dt.Columns.Add("商检金额", System.Type.GetType("System.Decimal"));
            dt.Columns.Add("应付商检费", System.Type.GetType("System.Decimal"));
            dt.Columns.Add("应收商检费", System.Type.GetType("System.Decimal"));

            DataSet ds = m_bllFeeTab.GetFeeNameList("FeeCateTab.FeeCateName like " + "'%" + "商检" + "%'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    string strTemp = ds.Tables[0].Rows[i]["FeeName"].ToString();
                    dt.Columns.Add(strTemp, System.Type.GetType("System.Decimal"));
                }
            }

            dt.Columns.Add("应收商检费合计", System.Type.GetType("System.Decimal"));


            dt.Columns.Add("运输公司", System.Type.GetType("System.String"));
            dt.Columns.Add("应付运输费", System.Type.GetType("System.Decimal"));
            ds = m_bllFeeTab.GetFeeNameList("FeeCateTab.FeeCateName like " + "'%" + "运输" + "%'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    string strTemp = ds.Tables[0].Rows[i]["FeeName"].ToString();
                    dt.Columns.Add(strTemp, System.Type.GetType("System.Decimal"));
                }
            }
            dt.Columns.Add("应收运输费合计", System.Type.GetType("System.Decimal"));


            dt.Columns.Add("报关行", System.Type.GetType("System.String"));
            dt.Columns.Add("报关单号", System.Type.GetType("System.String"));
            dt.Columns.Add("核销单号", System.Type.GetType("System.String"));
            dt.Columns.Add("报关金额", System.Type.GetType("System.Decimal"));
            dt.Columns.Add("币种", System.Type.GetType("System.String"));
            dt.Columns.Add("经营单位", System.Type.GetType("System.String"));
            dt.Columns.Add("申报日期", System.Type.GetType("System.DateTime"));
            dt.Columns.Add("应付报关费", System.Type.GetType("System.Decimal"));

            ds = m_bllFeeTab.GetFeeNameList("FeeCateTab.FeeCateName like " + "'%" + "报关" + "%'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    string strTemp = ds.Tables[0].Rows[i]["FeeName"].ToString();
                    dt.Columns.Add(strTemp, System.Type.GetType("System.Decimal"));
                }
            }
            dt.Columns.Add("应收报关费合计", System.Type.GetType("System.Decimal"));

            dt.Columns.Add("公帐汇款船务费", System.Type.GetType("System.Decimal"));
            dt.Columns.Add("私帐汇款船务费", System.Type.GetType("System.Decimal"));

            ds = m_bllFeeTab.GetFeeNameList("FeeCateTab.FeeCateName like " + "'%" + "单证" + "%'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    string strTemp = ds.Tables[0].Rows[i]["FeeName"].ToString();
                    dt.Columns.Add(strTemp, System.Type.GetType("System.Decimal"));
                }
            }

            dt.Columns.Add("应收单证费用合计", System.Type.GetType("System.Decimal"));
            dt.Columns.Add("应付单证费用合计", System.Type.GetType("System.Decimal"));

            ds = m_bllFeeTab.GetFeeNameList("FeeCateTab.FeeCateName like " + "'%" + "其他" + "%'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    string strTemp = ds.Tables[0].Rows[i]["FeeName"].ToString();
                    dt.Columns.Add(strTemp, System.Type.GetType("System.Decimal"));
                }
            }

            dt.Columns.Add("应收其他费用合计", System.Type.GetType("System.Decimal"));
            dt.Columns.Add("应付其他费用合计", System.Type.GetType("System.Decimal"));
        }

        public static void FillDataTable(DataTable dtNew, DataSet ds, Dictionary<string, DataRow> dicContractNo2DataRow)
        {
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    DataRow dr = dtNew.NewRow();
                    int nBGDComeBackTabID = Convert.ToInt32(ds.Tables[0].Rows[i]["BGDComeBackTabID"].ToString(), 10);
                    DataSet ds2 = m_bllCargoBoxTab.GetAllCargoBoxInfo("CargoBoxTab.BGDComeBackTabID = " + nBGDComeBackTabID.ToString());
                    if (ds2.Tables.Count > 0 && ds2.Tables[0].Rows.Count > 0)
                    {
                        dr["柜数"] = ds2.Tables[0].Rows.Count;
                        StringBuilder strSql = new StringBuilder();
                        for (int j = 0; j < ds2.Tables[0].Rows.Count; j++)
                        {
                            strSql.Append(ds2.Tables[0].Rows[j]["集装箱号"].ToString());
                            strSql.Append("\n");
                        }
                        dr["柜号"] = strSql.ToString();
                    }
                    else
                    {
                        dr["柜数"] = 0;
                        dr["柜号"] = "";
                    }


                    dr["合同号"] = ds.Tables[0].Rows[i]["合同编号"];
                    dr["商检金额"] = ds.Tables[0].Rows[i]["商检金额"];
                    dr["应付商检费"] = ds.Tables[0].Rows[i]["应付商检金额"];
                    dr["应收商检费"] = ds.Tables[0].Rows[i]["应收商检金额"];

                    dr["经营单位"] = ds.Tables[0].Rows[i]["经营单位"];
                    dr["订单号"] = ds.Tables[0].Rows[i]["订单号"];
                    dr["工厂简称"] = ds.Tables[0].Rows[i]["工厂名称"];
                    dr["报关行"] = ds.Tables[0].Rows[i]["报关行名称"];
                    dr["报关单号"] = ds.Tables[0].Rows[i]["报关单号"];
                    dr["核销单号"] = ds.Tables[0].Rows[i]["核销单号"];
                    dr["币种"] = ds.Tables[0].Rows[i]["币种"];
                    dr["申报日期"] = ds.Tables[0].Rows[i]["申报日期"];

                    dr["报关金额"] = ds.Tables[0].Rows[i]["报关金额"];
                    dr["运输公司"] = ds.Tables[0].Rows[i]["运输公司"];
                    dr["经营单位"] = ds.Tables[0].Rows[i]["单证公司"];
                    dr["公帐汇款船务费"] = ds.Tables[0].Rows[i]["公帐汇款船务费"];
                    dr["私帐汇款船务费"] = ds.Tables[0].Rows[i]["私帐汇款船务费"];
                    dr["运输公司"] = ds.Tables[0].Rows[i]["运输公司"];
                    dr["应付运输费"] = ds.Tables[0].Rows[i]["应付运输费"];

                    dr["应付报关费"] = ds.Tables[0].Rows[i]["应付报关费"];
                    dr["应付单证费用合计"] = ds.Tables[0].Rows[i]["应付单证费"];
                    dr["应付其他费用合计"] = ds.Tables[0].Rows[i]["应付其他费"];

                    int nSelectFacFeeTabID = Convert.ToInt32(ds.Tables[0].Rows[i]["FacFeeTabID"].ToString(), 10);
                    DataSet ds3 = m_bllFacFeeRelationTab.GetFeeNameList("FacFeeTabID = " + nSelectFacFeeTabID.ToString() + " and FeeCateTab.FeeCateName like " + "'%" + "商检" + "%'");

                    if (ds3.Tables.Count > 0 && ds3.Tables[0].Rows.Count > 0)
                    {
                        Decimal dtTotalTemp = 0;
                        for (int k = 0; k < ds3.Tables[0].Rows.Count; k++)
                        {
                            string strTemp = ds3.Tables[0].Rows[k]["费用名称"].ToString();
                            dr[strTemp] = ds3.Tables[0].Rows[k]["费用金额"].ToString();
                            dtTotalTemp += Convert.ToDecimal(ds3.Tables[0].Rows[k]["费用金额"].ToString());

                        }

                        dr["应收商检费合计"] = (Decimal)dr["应收商检费"] + dtTotalTemp;
                    }

                    ds3 = m_bllFacFeeRelationTab.GetFeeNameList("FacFeeTabID = " + nSelectFacFeeTabID.ToString() + " and FeeCateTab.FeeCateName like " + "'%" + "运输" + "%'");

                    if (ds3.Tables.Count > 0 && ds3.Tables[0].Rows.Count > 0)
                    {
                        Decimal dtTotalTemp = 0;
                        for (int k = 0; k < ds3.Tables[0].Rows.Count; k++)
                        {
                            string strTemp = ds3.Tables[0].Rows[k]["费用名称"].ToString();
                            dr[strTemp] = ds3.Tables[0].Rows[k]["费用金额"].ToString();
                            dtTotalTemp += Convert.ToDecimal(ds3.Tables[0].Rows[k]["费用金额"].ToString());

                        }

                        dr["应收运输费合计"] = dtTotalTemp;
                    }

                    ds3 = m_bllFacFeeRelationTab.GetFeeNameList("FacFeeTabID = " + nSelectFacFeeTabID.ToString() + " and FeeCateTab.FeeCateName like " + "'%" + "报关" + "%'");

                    if (ds3.Tables.Count > 0 && ds3.Tables[0].Rows.Count > 0)
                    {
                        Decimal dtTotalTemp = 0;
                        for (int k = 0; k < ds3.Tables[0].Rows.Count; k++)
                        {
                            string strTemp = ds3.Tables[0].Rows[k]["费用名称"].ToString();
                            dr[strTemp] = ds3.Tables[0].Rows[k]["费用金额"].ToString();
                            dtTotalTemp += Convert.ToDecimal(ds3.Tables[0].Rows[k]["费用金额"].ToString());

                        }

                        dr["应收报关费合计"] = dtTotalTemp;
                    }
                    ds3 = m_bllFacFeeRelationTab.GetFeeNameList("FacFeeTabID = " + nSelectFacFeeTabID.ToString() + " and FeeCateTab.FeeCateName like " + "'%" + "单证" + "%'");

                    if (ds3.Tables.Count > 0 && ds3.Tables[0].Rows.Count > 0)
                    {
                        Decimal dtTotalTemp = 0;
                        for (int k = 0; k < ds3.Tables[0].Rows.Count; k++)
                        {
                            string strTemp = ds3.Tables[0].Rows[k]["费用名称"].ToString();
                            dr[strTemp] = ds3.Tables[0].Rows[k]["费用金额"].ToString();
                            dtTotalTemp += Convert.ToDecimal(ds3.Tables[0].Rows[k]["费用金额"].ToString());

                        }

                        dr["应收单证费用合计"] = dtTotalTemp;
                    }

                    ds3 = m_bllFacFeeRelationTab.GetFeeNameList("FacFeeTabID = " + nSelectFacFeeTabID.ToString() + " and FeeCateTab.FeeCateName like " + "'%" + "其他" + "%'");

                    if (ds3.Tables.Count > 0 && ds3.Tables[0].Rows.Count > 0)
                    {
                        Decimal dtTotalTemp = 0;
                        for (int k = 0; k < ds3.Tables[0].Rows.Count; k++)
                        {
                            string strTemp = ds3.Tables[0].Rows[k]["费用名称"].ToString();
                            dr[strTemp] = ds3.Tables[0].Rows[k]["费用金额"].ToString();
                            dtTotalTemp += Convert.ToDecimal(ds3.Tables[0].Rows[k]["费用金额"].ToString());

                        }

                        dr["应收其他费用合计"] = dtTotalTemp;
                    }


                    dr["百业装卸发票"] = (Decimal)dr["应收商检费合计"] + (Decimal)dr["应收报关费合计"] + (Decimal)dr["应收大陆船务费合计"];
                    dr["百业收据"] = (Decimal)dr["应收单证费用合计"] 
                        + (Decimal)dr["应收DHL费用合计"] + (Decimal)dr["香港应收运输费合计"] + (Decimal)dr["应收香港船务费合计"];
                    dr["拖车行发票"] = dr["应收运输费合计"];
                    dr["应收费用合计"] = (Decimal)dr["百业收据"] + (Decimal)dr["百业装卸发票"] + (Decimal)dr["运输发票"];
                    dr["应付费用"] = (Decimal)dr["应付商检费"] + (Decimal)dr["应付运输费"] + (Decimal)dr["应付报关费"]
                        + (Decimal)dr["应付单证费用合计"] + (Decimal)dr["应付其他费用合计"]
                        + (Decimal)dr["私帐汇款船务费"] + (Decimal)dr["公帐汇款船务费"];

                    dtNew.Rows.Add(dr.ItemArray);
                    if (dicContractNo2DataRow != null)
                    {
                        dicContractNo2DataRow.Add(ds.Tables[0].Rows[i]["合同编号"].ToString(), dr);
                    }
                }
            }
        }

    }
}
